/**
 * @NApiVersion 2.x
 * @NScriptType MapReduceScript
 */
define(
    ['N/record', 'N/error', 'N/search', 'N/runtime'
        , './so.source.creater.main'
    ],
    function (record, error, search, runtime, soCreater) {

        function getInputData() {
            log.audit({
                title: '任务执行',
                details: '任务执行'
            });
            var scriptObj = runtime.getCurrentScript();
            log.audit({
                title: 'Remaining governance units: ',
                details: scriptObj.getRemainingUsage()
            });
            var mySearch;
            try {
                var searchid = runtime.getCurrentScript().getParameter('custscript_so_source_convert_search_id');
                var pageSize = runtime.getCurrentScript().getParameter('custscript_so_source_convert_page_size');
                mySearch = search.load({id: searchid});
                return mySearch.run().getRange({start: 0, end: parseInt(pageSize)});
            } catch (e) {
                log.error({title: 'e', details: JSON.stringify(e)});
            }
        }

        function map(context) {
            /*log.debug({
             title: 'context',
             details: JSON.stringify(context)
             });*/
            try {
                var sourceId = JSON.parse(context.value).id;
                var order = JSON.parse(context.value).values['custrecord_platform_so_context'];
                var platform = JSON.parse(context.value).values['custrecord_platform_so_platform'];
                order = JSON.parse(order);

                var result = soCreater.method.createSalesOrderBySourceContext(order);

                log.audit({
                    title: platform + ': result',
                    details: JSON.stringify(result)
                });

                var status;
                var errorMSG = result.message;
                var storeId = result.storeId;
                if (result.success) {
                    status = 2;
                } else {
                    status = 3;
                    var msgindex = errorMSG.indexOf('anonymous');
                    if (msgindex > 0) {
                        errorMSG = errorMSG.substr(0, msgindex);
                    }
                }

                if (errorMSG == "\"order is exists\"") {
                    try {
                        record.delete({
                            type: 'customrecord_platform_so_source',
                            id: sourceId
                        });
                    } catch (e) {
                    }
                } else {
                    record.submitFields({
                        type: 'customrecord_platform_so_source',
                        id: sourceId,
                        values: {
                            custrecord_platform_so_craete_msg: JSON.stringify(result),
                            custrecord_platform_so_store: storeId,
                            custrecord_platform_so_error_msg: errorMSG,
                            custrecord_platform_so_status: status
                        }
                    });
                }


            } catch (e) {
                log.error({
                    title: 'e',
                    details: JSON.stringify(e)
                });
            }

            //return result;
        }

        function reduce(context) {

        }

        function summarize(summary) {
            var scriptObj = runtime.getCurrentScript();
            log.audit({
                title: 'Remaining governance units: ',
                details: scriptObj.getRemainingUsage()
            });
        }

        return {
            getInputData: getInputData,
            map: map,
            reduce: reduce,
            summarize: summarize
        };
    });